| Conditions | 4 |
| Total Lines | 56 |
| Code Lines | 44 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import { Inject } from '@nestjs/common'; |
||
| 33 | private async createNotification(command: CreateNotificationCommand): Promise<string> { |
||
| 34 | const { message, type, leaveReaquest } = command; |
||
| 35 | |||
| 36 | if (type === NotificationType.POST) { |
||
| 37 | const { id } = await this.mattermostNotifier.createPost( |
||
| 38 | MATTERMOST_CHANNEL_LEAVES_ID, |
||
| 39 | message |
||
| 40 | ); |
||
| 41 | const notification = await this.notificationRepository.save( |
||
| 42 | new Notification(type, message, id, leaveReaquest) |
||
| 43 | ); |
||
| 44 | |||
| 45 | return notification.getId(); |
||
| 46 | } |
||
| 47 | |||
| 48 | if (type === NotificationType.REACTION) { |
||
| 49 | const rootNotification = await this.getRootNotification( |
||
| 50 | leaveReaquest.getId() |
||
| 51 | ); |
||
| 52 | |||
| 53 | await this.mattermostNotifier.createReaction( |
||
| 54 | rootNotification.getResourceId(), |
||
| 55 | message |
||
| 56 | ); |
||
| 57 | |||
| 58 | const notification = await this.notificationRepository.save( |
||
| 59 | new Notification( |
||
| 60 | type, |
||
| 61 | message, |
||
| 62 | rootNotification.getResourceId(), |
||
| 63 | leaveReaquest |
||
| 64 | ) |
||
| 65 | ); |
||
| 66 | |||
| 67 | return notification.getId(); |
||
| 68 | } |
||
| 69 | |||
| 70 | if (type === NotificationType.COMMENT) { |
||
| 71 | const rootNotification = await this.getRootNotification( |
||
| 72 | leaveReaquest.getId() |
||
| 73 | ); |
||
| 74 | |||
| 75 | const { id } = await this.mattermostNotifier.createComment( |
||
| 76 | this.configService.get<string>('MATTERMOST_CHANNEL_LEAVES_ID'), |
||
| 77 | message, |
||
| 78 | rootNotification.getResourceId() |
||
| 79 | ); |
||
| 80 | |||
| 81 | const notification = await this.notificationRepository.save( |
||
| 82 | new Notification(type, message, id, leaveReaquest) |
||
| 83 | ); |
||
| 84 | |||
| 85 | return notification.getId(); |
||
| 86 | } |
||
| 87 | |||
| 88 | throw new Error('Type not managed'); |
||
| 89 | } |
||
| 98 |